#ifndef __MYSTRING__
#define __MYSTRING__
#include <iostream>
#include <cstring>
using namespace std;


class MyString
{
public:
    MyString(const char* tmp = 0);
    MyString(const MyString& tmp);
    MyString& operator= (const MyString& tmp);
    void showValue()const;
    char* getValue()const;
    void setValue(const char* str);
    ~MyString();

private:
    char *value;
};
 
inline
MyString::MyString(const char* tmp)
{
    if(tmp) {
        value = new char[strlen(tmp)+1];
        strcpy(value, tmp);
    }else {
        value = new char[1];
        *value = '\0';
    }
}

inline
MyString::MyString(const MyString& tmp){
    value = new char[strlen(tmp.value) + 1];
    strcpy(value,tmp.value);
}


inline
MyString& MyString::operator=(const MyString& tmp){
    if(this == &tmp) { 
        //检测自我赋值，防止自我赋值导致delete[] value时出错
        return *this;
    }else {
        delete[] value;
        value = new char[strlen(tmp.value)+1];
        strcpy(value,tmp.value);
        return *this;//返回这里不为空是为了连续赋值： a=b=c
    }

}

inline
void MyString::showValue()const{
    cout<<value<<endl;
}

inline
char* MyString::getValue()const{
    return value;
}

inline
void MyString::setValue(const char* str) {
    if (value != str) {
        delete[] value;
        value = new char[strlen(str)+1];
        strcpy(value,str);
    }
}

inline
MyString::~MyString(){
    delete[] value;
}

ostream& operator<<(ostream& os,MyString& str) {
    os<<str.getValue();
    return os;
}

istream& operator>>(istream& is,MyString& str) {
    string tmp;
    is >>tmp;
    str.setValue(tmp.c_str());
    return is;
}
#endif